package com;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Student extends JFrame {

	private static final long serialVersionUID = 1L;
	static int width = Toolkit.getDefaultToolkit().getScreenSize().width; // 获得屏幕宽带
	static int height = Toolkit.getDefaultToolkit().getScreenSize().height; // 获得屏幕高度
	private BufferedImage image;
	private static DatagramSocket ds;// 接收屏幕广播套接字
	private boolean isScreenCast;// 是否屏幕广播
	
	static{
		try {
			ds = new DatagramSocket(54529);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Student() {
		try {
			
			byte[] tmp = new byte[1024];
			DatagramPacket dp = new DatagramPacket(tmp, tmp.length);
			isScreenCast = true;
			new Thread() {
				@Override
				public void run() {
					while (isScreenCast) {
						try {
							ByteArrayOutputStream out = new ByteArrayOutputStream();
							while (true) {
								ds.receive(dp);
								String mess = new String(dp.getData(), 0, dp.getLength());
								if (mess.equals("end"))
									break;
								out.write(tmp, 0, dp.getLength());
							}
							byte[] imgb = out.toByteArray();
							InputStream in = new ByteArrayInputStream(imgb);
							image = ImageIO.read(in);
							repaint();
						} catch (IOException e) {
							//e.printStackTrace();
						}
					}
				}
			}.start();
			this.setUndecorated(true); // 全屏显示
			GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void sendCmd(String cmdStr, DatagramPacket dp) {
		try {
			DatagramSocket socket = new DatagramSocket();
			byte[] tmp = new byte[8192];
			DatagramPacket cmd = new DatagramPacket(tmp, tmp.length, dp.getAddress(), dp.getPort());
			cmd.setData(cmdStr.getBytes());
			socket.send(cmd);
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void paint(Graphics g) {//画界面
		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(image, 0, 0, width, height, this);
	}

	@Override
	public void update(Graphics g) {
		Graphics gBuffer = image.getGraphics();// 获得图片缓冲区的画笔
		if (gBuffer != null)
			paint(gBuffer);
		else
			paint(g);
		gBuffer.dispose();
		g.drawImage(image, 0, 0, width, height, this);
	}

	public void setScreenCast(boolean isScreenCast) {
		this.isScreenCast = isScreenCast;
	}
}
